package com.fang.mmy.utils;

/**
 * @ClassName BitMap
 * @Author kebukeyi
 * @Date 2022/8/8 18:40
 * @Description
 * @Version 1.0.0
 */
public class BitMap {

    private byte[] bytes;
    private int maxValue;
    private int capacity;

    public BitMap(int maxValue) {
        this.maxValue = maxValue;
        this.capacity = (maxValue / 8) + 1;
        this.bytes = new byte[capacity];
    }

    public boolean contains(int value) {
        if (value <= maxValue) {
            int index = value / 8;
            int position = value % 8;
            int temBit = 1 << position;
            return ((bytes[index] & temBit)) != 0;
        }
        return false;
    }

    public boolean add(int value) {
        if (value <= maxValue) {
            int index = value / 8;
            int position = value % 8;
            int temBit = 1 << position;
            bytes[index] = (byte) (bytes[index] | temBit);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        BitMap bitMap = new BitMap(128);
        System.out.println(bitMap.contains(10));
        bitMap.add(12);
        bitMap.add(22);
        bitMap.add(14);
        System.out.println(bitMap.contains(11));
        System.out.println(bitMap.contains(12));
    }


}
